package com.zhz.lib.project.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.zhz.lib.project.controller.form.BookreviewForm;
import com.zhz.lib.project.domain.news.Sortmap;
import com.zhz.lib.project.ibatis.domain.bookreview.Bookreview;
import com.zhz.lib.project.service.news.BookreviewService;
import com.zhz.lib.project.vo.bookreview.BookreviewVO;
import com.zhz.project.common.util.ibatispage.IbatisPage;
import com.zhz.project.common.util.zhzutil.ZhzBeanUtils;
import com.zhz.project.common.util.zhzutil.ZhzHttpRequestUtil;

/**
 * @author LiWei <a href="mailto:liwei2672@gmail.com">liwei2672@gmail.com</a>
 * @version 创建时间：2010-5-3 下午03:22:27
 * 
 */
@Controller
@RequestMapping("/bookreview.do")
public class BookreviewController {
    private static Logger     logger   = Logger.getLogger(BookreviewController.class);
    @Autowired
    private BookreviewService bookreviewService;
    @Autowired
    private IbatisPage        ibatisPage;

    private static String     pageSql  = "selectAllBookreviewByPage";
    private static String     countSql = "selectAllBookreviewCount";

    @RequestMapping(method = RequestMethod.POST)
    public String getIbatisListPostNoAction(ModelMap model, BookreviewForm bookreviewForm) {
        return page(model, bookreviewForm);
    }

    @RequestMapping(method = RequestMethod.GET)
    public String getIbatisListGetNoAction(ModelMap model, BookreviewForm bookreviewForm) {
        return page(model, bookreviewForm);
    }

    @RequestMapping(method = RequestMethod.POST, params = "action=page")
    public String getIbatisListPost(ModelMap model, BookreviewForm bookreviewForm) {
        return page(model, bookreviewForm);
    }

    @RequestMapping(method = RequestMethod.GET, params = "action=page")
    public String getIbatisListGet(ModelMap model, BookreviewForm bookreviewForm) {
        return page(model, bookreviewForm);
    }

    @SuppressWarnings("unchecked")
    private String page(ModelMap model, BookreviewForm bookreviewForm) {
        ZhzHttpRequestUtil.writeMapToModelMap(model, ibatisPage.getPage(ZhzBeanUtils
            .describe(bookreviewForm), countSql, pageSql));

        // 将Bookreview转换为BookreviewVO
        List<Bookreview> bookreviewList = (List<Bookreview>) model.get("list");
        List<BookreviewVO> bookreviewVOList = new ArrayList<BookreviewVO>();
        if (bookreviewList != null && bookreviewList.size() > 0) {
            for (Bookreview bookreview : bookreviewList) {
                BookreviewVO bookreviewVO = new BookreviewVO();
                BeanUtils.copyProperties(bookreview, bookreviewVO);
                bookreviewVOList.add(bookreviewVO);
            }
        }
        model.put("list", bookreviewVOList);

        // 获取新书类型
        List<Sortmap> typeList = bookreviewService.getBookreviewType();
        model.put("typeList", typeList);

        return "bookreviewlist";
    }

    /**
     * 
     * 根据id获取最新消息
     * 
     * @param model
     * @param request
     * @param response
     * @param id
     * @return
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(method = RequestMethod.GET, params = "event=single")
    public String getSingleBookreview(ModelMap model, @RequestParam("id") int id) {
        model.put("bookreview", bookreviewService.getBookreviewById(id));
        bookreviewService.bookreviewHitPlusOne(id);
        return "bookreviewDetail";
    }

}
